<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Default_Model_Donhang extends ZF_DB{
    protected $_table_name = "donhang" ;
    
    protected $_madonhang;
    protected $_madailybanhang;
    protected $_madailymua;
    protected $_madailygioithieu;
    protected $_dsmactdh;
    protected $_tongthue;
    protected $_tongtien;
    protected $_tongthanhtien;
    protected $_ngaymuahang;
    
    public function getMaDonHang(){
        return $this->_madonhang;
    }
    public function setMaDonHang($madonhang){
        $this->_madonhang = $madonhang;
    }
    public function getMaDaiLyBanHang(){
        return $this->_madailybanhang;
    }
    public function setMaDaiLyBanHang($madailybanhang){
        $this->_madailybanhang = $madailybanhang;
    }
    public function getMaDaiLyMua(){
        return $this->_madailymua;
        
    }
    public function setMaDaiLyMua($madailymua){
       $this->_madailycha = $madailymua;
        
    }
    public function getMaDaiLyGioiThieu(){
        return $this->_madailygioithieu;
        
    }
    public function setMaDaiLyGioiThieu($madailygioithieu){
        $this->_madailygioithieu = $madailygioithieu;
    }
    public function getDsMaCTDH(){
        return $this->_dsmactdh;
    }
    public function setDsMaCTDH($dsmactdh){
        $this->_dsmactdh = $dsmactdh;
    }
    public function getTongThue(){
        return $this->_tongthue;
    }
    public function setTongThue($tongthue){
        $this->_tongthue = $tongthue;
    }
    public function getTongTien(){
        return $this->_tongtien;
        
    }
    public function setTongTien($tongtien){
        $this->_tongtien = $tongtien;
    }
    public function getTongThanhTien(){
        return $this->_tongthanhtien;
    }
    public function setTongThanhTien($tongthanhtien){
         $this->_tongthanhtien = $tongthanhtien;
     }
    public function getNgayMuaHang(){
       return $this->_ngaymuahang;
   }
    public function setNgayMuaHang($ngaymuahang){
        $this->_ngaymuahang = $ngaymuahang;
    }
    
    public function insert($madailybanhang, $tongthanhtien, $ngaymuahang, $madailymua = "", $madailygioithieu = "", $dsmactdh = "", $tongtien = 0, $tongthue = 0){
        
        if($madailybanhang && $tongthanhtien && $ngaymuahang){             
                $data =  array(
                    'MaDaiLyBan'=>$madailybanhang,
                    'MaDaiLyMua' =>$madailymua,
                    'MaDaiLyGioiThieu'=>$madailygioithieu,
                    'DSMaCTDH'=>$dsmactdh,
                    'TongThue'=>$tongthue,
                    'TongTien'=>$tongtien,
                    'TongThanhTien' =>$tongthanhtien,
                    'NgayMuaHang'=>$ngaymuahang);
            if ($this->db->insert($this->_table_name,$data) == 1)
            {
                return $this->db->lastInsertId();
            }
	}
        return -1;
	
    }
   
    public function update(){
        $where  = "id = $this->_id";
	$data =  array(
                    'name' =>$this->_name,
                    'name_en'=>$this->_name_en,
                    'address' =>$this->_address,
                    'phone'=>$this->_phone,
                    'description'=>$this->_description,
                    'description_en'=>$this->_description_en,
                    'city'=>$this->_city,
                    'images'=>$this->_images,
                    'active'=>$this->_active);
        
	return $this->db->update(
                $this->_table_name,
                $data,$where);
    }
    public function getById($id){
        if($id)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.MaDonHang = ?",$id );
		
		$data = $this->db->fetchRow($select);
		if($data){
                        $this->_madonhang = $data['MaDonHang'];
			$this->_madailybanhang = $data['MaDaiLyBan'];
			$this->_madailymua = $data['MaDaiLyMua'];
			$this->_madailygioithieu = $data['MaDaiLyGioiThieu'];
			$this->_dsmactdh = $data['DSMaCTDH'];
                        $this->_tongthue = $data['TongThue'];       
                        $this->_tongtien = $data['TongTien'];
                        $this->_tongthanhtien = $data['TongThanhTien'];
                        $this->_ngaymuahang = $data['NgayMuaHang'];
		}
                return $data;
	}
	return -1;
    }
   
    public function listAll(){
        $select = $this->db->select()
                ->from($this->_table_name);
        $data = $this->db->fetchAll($select);
	return $data;
    }
    public function delete(){
            $this->getById($this->_id);
            $this->_active = 0;
            return $this->update();
    }
}
?>
